/*  
 *  @author Philip Stutz
 * 
 *  Copyright 2011 University of Zurich
 *      
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *         http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ch.uzh.ifi.ddis.ds2011.a3.helper;

import java.util.List;
import java.util.Set;

import org.openrdf.rio.RDFHandler;

import com.signalcollect.Graph;

/**
 * The class StreamingHelper simplifies streaming triples to vertices.
 */
public class StreamingHelper {

	/**
	 * Streams triples loaded from the N-Triples file `tripleFilePath` to
	 * vertices which haven a vertex id that is contained in `vertexIds`.
	 * 
	 * @param tripleFilePath
	 *            the file path of the N-Triples file
	 * @param g
	 *            graph that contains the vertices with ids `vertexIds`
	 * @param vertexIds
	 *            the vertex ids of the vertices that will receive the triples
	 *            stream
	 */
	public static void streamTriplesToVertices(String tripleFilePath,
			final Graph g, final List<?> vertexIds) {
		RDFHandler tripleStreamHandler = new TripleHandler() {
			public void handleTriple(Triple triple) {
				Set<Triple> tripleSet = CollectionBuilder.newSet(triple);
				for (Object vertexId : vertexIds) {
					g.sendSignalToVertex(tripleSet, vertexId);
				}
			}
		};
		ParseHelper.parseNTriplesFileWithHandler(tripleFilePath,
				tripleStreamHandler);
	}
}
